package com.xms.file.service;

import com.xms.core.config.SystemEntityNames;
import com.xms.core.data.Entity;
import com.xms.core.model.PagedList;
import com.xms.sdk.core.query.ConditionOperator;
import com.xms.sdk.core.query.OrderType;
import com.xms.sdk.core.query.PagingInfo;
import com.xms.sdk.core.query.QueryExpression;
import com.xms.sdk.service.IDataFinder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * <p>
 * AttachmentFinderService
 * </p>
 *
 * @author migo
 * @since 2021/9/26
 */
@Service
public class AttachmentFinderService implements IAttachmentFinderService{
    private IDataFinder _dataFinder;
    @Autowired
    public void AttachmentFinderService(IDataFinder dataFinder) {
        _dataFinder = dataFinder;
    }

    @Override
    public Entity getById(UUID id) throws Exception {
        return _dataFinder.retrieveById(SystemEntityNames.Attachment, id);
    }

    @Override
    public PagedList<Entity> page(int page, int pageSize, UUID entityId, UUID objectId) throws Exception {
        QueryExpression query = new QueryExpression(SystemEntityNames.Attachment);
        query.PageInfo = new PagingInfo(page, pageSize);
        query.ColumnSet.addColumns("attachmentid", "name", "cdnpath", "description", "createdon", "createdby", "ownerid", "owningbusinessunit");
        query.Criteria.addCondition("entityid", ConditionOperator.Equal, entityId);
        query.Criteria.addCondition("objectid", ConditionOperator.Equal, objectId);
        query.AddOrder("createdon", OrderType.Descending);
        return _dataFinder.retrieveMultiple(query);
    }
}
